package com.service.impl;

import com.dto.QueryDto;
import com.dto.ShangJiaDto;
import com.mapper.ShangJiaMapper;
import com.pojo.ShangJia;
import com.service.ShangJiaService;
import com.utils.MapUtils;
import com.utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class ShangJiaServiceImpl implements ShangJiaService {
    @Autowired
    private ShangJiaMapper shangJiaMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Override
    public Boolean addShangJia(ShangJiaDto shangJiaDto) {
        ShangJia shangjia=new ShangJia();
        BeanUtils.copyProperties(shangJiaDto,shangjia);
        shangjia.setCreateTime(LocalDateTime.now().toString());
        shangjia.setShangId(snowFlake.nextId()+"");
        return shangJiaMapper.insertSJ(shangjia);
    }

    @Override
    public List<ShangJia> userquerynearby(QueryDto queryDto) {
        List<ShangJia> list=shangJiaMapper.queryAllShangJia();
        List<ShangJia> res=new ArrayList<>();
        double min=Double.MAX_VALUE;
        double min1=Double.MAX_VALUE;
        double min2=Double.MAX_VALUE;
        double min3=Double.MAX_VALUE;
        double min4=Double.MAX_VALUE;
        for (ShangJia s:list){
            Double juli = MapUtils.GetDistance(s.getLongitude(), s.getLongitude(), queryDto.getLongitude(), queryDto.getLatitude());
            if(juli<min){
                res.add(s);
                min=juli;
            }else if(juli<min1){
                res.add(s);
                min1=juli;
            }else if(juli<min2){
                res.add(s);
                min2=juli;
            }else if(juli<min3){
                res.add(s);
                min3=juli;
            }else if(juli<min4){
                res.add(s);
                min4=juli;
            }
        }
        return res;
    }
}
